using System;
using System.Collections;
using System.Collections.Generic;
using PEProtocol;
using UnityEngine;
using UnityEngine.UI;

public class UI_Create : WindowRoot
{
    private AcctSystem mAccSystem;
    private Transform mItemRoot;
    private List<RoleData> mRoleDatas;
    private Button mLoginBtn;

    private List<Image> mSelectImgs;
    private int mSelectUID;

    private void Awake()
    {
        mItemRoot = GetTrans(transform, "LeftPin/IMG_FrameItem/bgInner");
        mLoginBtn = transform.Find("BTN_Login").GetComponent<Button>();
        mLoginBtn.onClick.AddListener(() =>
        {
            NetMsg msg = new NetMsg();
            msg.CMD = CMD.ReqRoleToken;
            msg.ReqRoleToken = new ReqRoleToken();
            msg.ReqRoleToken.Acct = mRoot.AcctSystem.Account;
            msg.ReqRoleToken.SelectUID = mSelectUID;
            mRoot.NetService.SendMsg(msg,(rsp =>
            {
                if (rsp.EID == ErrorCode.None)
                {
                    var rspToken = rsp.RspRoleToken;
                    mRoot.AcctSystem.IP =  rspToken.BattleIP;
                    mRoot.AcctSystem.Port = rspToken.BattlePort;
                    mRoot.AcctSystem.Token = rspToken.Token;
                    mRoot.AcctSystem.SelectUID = mSelectUID;
                    mRoot.NetService.StartConnectToBattle();
                }
                else
                {
                    this.Error($"CMD: Rsp {rsp.CMD} error,reason:[{rsp.EID.ToString()}]");
                }

            }));
        });
    }

    public override void InitWindow()
    {
        base.InitWindow();
        mEvtSvc.AddMsgHandler(EvtID.OnRoleDataUpdate, RefreshUI);
        mAccSystem = mRoot.AcctSystem;
        mRoleDatas = mAccSystem.RoleDatas;
        if (mRoleDatas != null)
        {
            RefreshUI();
        }
        else
        {
            mItemRoot.GetChild(0).gameObject.SetActive(false);
        }
    }

 
    public void RefreshUI(object obj1 = null, object obj2 = null)
    {
        if (obj1 != null)
        {
            mRoleDatas = (List<RoleData>)obj1;
        }
        mSelectImgs = new List<Image>(mRoleDatas.Count);
        mItemRoot.EnsureChild(mRoleDatas.Count);
        mItemRoot.ShowChild(OnRoleDataUpdate);
        SelectImg(0);
    }

    public void OnRoleDataUpdate(int index, Transform f)
    {
        RoleData data = mRoleDatas[index];
        Button selectBtn = f.GetComponent<Button>();
        Image selectImg = f.transform.Find("IMG_Selected").GetComponent<Image>();
        f.transform.Find("bgName/TXT_Name").GetComponent<Text>().text = data.NickName;
        mSelectImgs.Add(selectImg);
        selectImg.gameObject.SetActive(false);
        selectBtn.onClick.AddListener(() =>
        {
            SelectImg(index);
            mSelectUID = data.UID;

        });
    }

    public void HideAllSelectImg()
    {
        foreach (Image img in mSelectImgs)
        {
            img.gameObject.SetActive(false);
        }
    }

    public void SelectImg(int index)
    {
        if (mSelectImgs.Count > 0)
        {
            HideAllSelectImg();
            mSelectImgs[index].gameObject.SetActive(true);
        }
    }
}